package com.xf.rigister;

import com.xf.common.Invocation;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import java.util.Iterator;
public class RemoteRigister {
    //private static HashMap<String, HashMap<String,Vector<Invocation>>> serviceinfo ;
    private static HashMap<String,HashMap<String,Vector<String>>> selectinfo;


    public static HashMap<String, HashMap<String, Vector<String>>> getSelectinfo() {
        return selectinfo;
    }



//    public static HashMap<String, HashMap<String,Vector<Invocation>>> getServiceinfo() {
//        return serviceinfo;
//    }
    public static Boolean Isservice_exist(Invocation invocation,String address,Integer port){
        if(selectinfo.containsKey(invocation.getInterfaceName()+invocation.getVersion())){
            if(selectinfo.get(invocation.getInterfaceName()+invocation.getVersion()).containsKey(invocation.getMethod())){
                Vector<String> vector=selectinfo.get(invocation.getInterfaceName()
                        +invocation.getVersion()).get(invocation.getMethod());
                for(String s:vector){
                    if(s.equals(address+":"+port)){
                        return true;
                    }
                }
            }
        }
        return false;
    }
//    public static Boolean Isservice_exist(Invocation invocation,String address,Integer port) {
//        if(serviceinfo.containsKey(address + ":" + port)){
//            if(serviceinfo.get(address + ":" + port).containsKey(invocation.getInterfaceName() + invocation.getVersion())){
//                Vector<Invocation> invocations = serviceinfo.get(address + ":" + port).get(invocation.getInterfaceName() + invocation.getVersion());
//                for( Invocation item:invocations){
//                    if(item.getMethodName().equals(invocation.getMethodName())&&item.getParameterTypes().length==invocation.getParameterTypes().length){
//                        for(int i=0;i<item.getParameterTypes().length;i++){
//                            if(!item.getParameterTypes()[i].getName().equals(invocation.getParameterTypes()[i].getName())){
//                                return false;
//                            }
//                        }
//                        return true;
//                    }
//                }
//            }
//        }
//        return false;
//    }

    public static void rigist(Invocation invocation,String address,Integer port) {

        if(!selectinfo.containsKey(invocation.getInterfaceName()+invocation.getVersion())){
            HashMap<String,Vector<String>> innerHashMap = new HashMap<>();
            selectinfo.put(invocation.getInterfaceName()+invocation.getVersion(),innerHashMap);
        }
        if(!selectinfo.get(invocation.getInterfaceName()+invocation.getVersion()).containsKey(invocation.getMethod())){
            HashMap<String,Vector<String>> innerHashMap = selectinfo.get(invocation.getInterfaceName()+invocation.getVersion());
            Vector<String> innervector=new Vector<String>();
            innerHashMap.put(invocation.getMethod(),innervector);
        }
        Vector<String> vector=selectinfo.get(invocation.getInterfaceName()+invocation.getVersion()).get(invocation.getMethod());
        vector.add(address+":"+port);


//        if(!serviceinfo.containsKey(address+":"+port)){
//            HashMap<String, Vector<Invocation>> innerHashMap = new HashMap<>();
//            serviceinfo.put(address+":"+port,innerHashMap);
//        }
//        if(!serviceinfo.get(address+":"+port).containsKey(invocation.getInterfaceName()+invocation.getVersion())){
//            HashMap<String, Vector<Invocation>> innerHashMap = serviceinfo.get(address+":"+port);
//            Vector<Invocation> innervector=new Vector<Invocation>();
//            innerHashMap.put(invocation.getInterfaceName()+invocation.getVersion(),innervector);
//        }
//        Vector<Invocation> innervector=serviceinfo.get(address+":"+port).get(invocation.getInterfaceName()+invocation.getVersion());
//        innervector.add(invocation);
    }



    public static void saveinfo()  {
        ObjectOutputStream fileos = null;
        try {
            fileos = new ObjectOutputStream(new FileOutputStream("information.txt"));
            //fileos.writeObject(serviceinfo);

            fileos.writeObject(selectinfo);
            fileos.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void loadinfo() {
        //serviceinfo=new HashMap<>();

        selectinfo=new HashMap<>();
        ObjectInputStream fileis = null;
        try {
            fileis = new ObjectInputStream(new FileInputStream("information.txt"));
            //serviceinfo= (HashMap<String, HashMap<String, Vector<Invocation>>>) fileis.readObject();

            selectinfo= (HashMap<String, HashMap<String, Vector<String>>>) fileis.readObject();
            fileis.close();
            //System.out.println("读取成功");
        } catch (IOException | ClassNotFoundException e) {
            System.out.println("读取本地文件信息失败！");
        }
    }
}
